package com.justnow.offer;

import java.util.*;

/**
 * @author justnow
 * Created on 2020-08-23
 * Description
 */
public class Solution5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        scanner.nextLine();
        while (scanner.hasNext()) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int num = recycleNum(s, start, end);
            System.out.println(num);
        }
    }

    public static boolean isRecycle(String s) {
        int length = s.length();
        boolean flag = true;
        for (int i = 0; i < length / 2; i++) {
            int last = length - 1 - i;
            if (s.charAt(i) != s.charAt(last)) {
                flag = false;
                break;
            }
        }
        return flag;
    }


    public static int recycleNum(String s, int start, int end) {
        int res = 0;
        if (start + 1 == end) {
            res = 1;
            return res;
        }
        int small = Integer.MAX_VALUE;

        //字符串的拆分问题
        for (int i = start; i <= end; i++) {
            for (int j = i + 1; j <= end ; j++) {
                int num = 0;
                if (isRecycle(s.substring(i, j + 1)))
                    num++;
                if (isRecycle(s.substring(j, end + 1)))
                    num++;
                small = Math.min(small, num);
            }
        }
        res = small;
        return res;
    }
}
